package com.lanou3g.controller;

import com.lanou3g.entity.Phone;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
@RequestMapping("/phone")
public class PhoneController {
    @RequestMapping("/add")
    public void add(@Valid Phone phone, BindingResult br) {
//@Valid与@Validated是一样的,用谁都行
        //Phone phone, BindingResult bindingResult是一组的,他俩中间不能放东西,如果要判断别的类,在这样写一组
        if (br.hasErrors()) {//br.hasErrors()是否有错误,如果br有错,就为false,否则为true
            System.out.println("没有通过校验");
//            FieldError brandError = br.getFieldError("brand");//获取brand属性的字段的错误
//            System.out.println(brandError.getDefaultMessage());
            //brandError.getDefaultMessage()获取默认信息,即前边你规定的注解的要求信息
        }else{
            System.out.println("通过了校验");
        }
    }
}
